#pragma once

#include <iostream>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <boost/algorithm/string.hpp>

namespace rs_util
{
    const std::string tem_path = "./temfile/";
    class TimeUtil
    {
    public:

        static std::string GetTimestamp()
        {
            struct timeval _tv;
            gettimeofday(&_tv,nullptr);
            return std::to_string(_tv.tv_sec);  
        }

        static std::string GetTimeMs()
        {
            struct timeval _tv;
            gettimeofday(&_tv,nullptr);
            return std::to_string(_tv.tv_sec*1000+_tv.tv_usec/1000);  
        }

    };
    class PathUtil
    {
    public:
        static std::string AddPath(const std::string &file_path, const std::string &suffix)
        {
            std::string ret_path = tem_path;
            ret_path += file_path;
            ret_path += suffix;
            return ret_path;
        }
        //编译时所需临时文件
        static std::string SrcFile(const std::string &file_name)
        {
            return AddPath(file_name, ".cpp");
        }

        static std::string ExeFile(const std::string &file_name)
        {
            return AddPath(file_name, ".exe");
        }

        static std::string ErrFile(const std::string &file_name)
        {
            return AddPath(file_name, ".complie_err");
        }

        //运行时所需临时文件
        static std::string Stdin(const std::string&file_name)
        {
            return AddPath(file_name,".stdin");
        }
        static std::string Stdout(const std::string&file_name)
        {
            return AddPath(file_name,".stdout");
        }
        static std::string Stderr(const std::string&file_name)
        {
            return AddPath(file_name,".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExist(const std::string file_path)
        {
            struct stat st;
            if (stat(file_path.c_str(), &st) == 0)
            {
                return true;
            }
            return false;
        }

        static std::string FileUniq()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetTimeMs();
            std::string sid = std::to_string(id);

            return ms + "_" +sid;
        }

        static bool WriteFile(const std::string& target,const std::string&content)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }

            out.write(content.c_str(),content.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string& target,std::string* content,bool keep = false)
        {
           (*content).clear();

            std::ifstream in(target);
            if(!in.is_open())
            {
                return false;
            }

            std::string line;
            while(std::getline(in,line))
            {
                *content += line;
                *content += (keep?"\n":"");
            }
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string& str,std::vector<std::string>* target,const std::string&sep)
        {
           boost::split((*target),str,boost::is_any_of(sep),boost::algorithm::token_compress_on);
        }
    };
}